package com.rakuten.api.mule.transformers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.common.metadata.datatype.DataTypeFactory;
import org.mule.transformer.AbstractMessageTransformer;

import com.rakuten.api.mule.model.DisplayStartDate;
import com.rakuten.api.mule.model.FindShopInfo;
import com.rakuten.api.mule.model.Price;
import com.rakuten.api.mule.model.Product;
import com.rakuten.api.mule.model.Variants;
import com.rakuten.api.mule.model.item.Item;
import com.rakuten.api.mule.model.item.ItemDimension;
import com.rakuten.api.mule.model.item.ItemDuration;
import com.rakuten.api.mule.model.item.ItemImage;
import com.rakuten.api.mule.model.item.ItemPricing;
import com.rakuten.api.mule.model.item.ItemUpsertRequest;
import com.rakuten.api.mule.model.item.ItemVariant;
import com.rakuten.api.mule.model.item.ItemWeight;
import com.rakuten.api.mule.model.item.ItemVariant.ShopInfo;
import com.rakuten.api.mule.model.item.MultiLang;
import com.rakuten.api.mule.model.shop.Shop;

public class MapProductToItem extends AbstractMessageTransformer {
    public MapProductToItem() {
//	registerSourceType(DataTypeFactory.STRING);

//	setReturnDataType(DataTypeFactory.create(ItemUpsertRequest.class));	
    }
    
	@Override
	public Object transformMessage(MuleMessage message, String outputEncoding)
			throws TransformerException {
		//read product data
		Product product = message.getProperty("productInfo",
				PropertyScope.SESSION);
		String merchantId = message.getProperty("merchantId",
				PropertyScope.SESSION);
		// Prepare Variants
		List<ItemVariant> variantsListItem = new ArrayList<ItemVariant>(); 
		
		//read shop info from payload
		Map<String, FindShopInfo> findShopInfoMap = message.getProperty(
				"findShopInfoMap", PropertyScope.SESSION);
		 
		//read variant list
		List<Variants> productVariantList = (List<Variants>)product.getVariants();
		
		Item item = new Item();
		// Sku OK 	
		item.setBaseSku(product.getBaseSku());
		// Brand OK
		item.setBrand(product.getBrand());
		
		// set discriptionhtml OK
		item.setDescriptionHtml(product.getDescriptions());
		
		//★★★★★★★★★★★★★★★★★★★★★★★jo add for test
		item.setAdultOnly(false);
		item.setGiftWrappable(false);
		item.setInventoryHidden(false);
		item.setInactiveTime("2013-12-12 10:10:10.001");
		//item.setSearchableStartTime("")
//		// setting first DisplayStartDate object value OK
//		List<DisplayStartDate> displayStartDate = product.getDisplayStartDates();
//		DisplayStartDate temp =  displayStartDate.get(0);
//		ItemDuration itemDuration = new ItemDuration();
//		itemDuration.setLiveStartTime(displayStartDate.get(0).getDisplayStartDate());
		
		//isFreeShipping OK
		item.setFreeShipping(product.isFreeShipping());
		//Name OK
		item.setName(product.getTitles());
		// RakutenCatagoryId OK 
		item.setRakutenCategoryId(Integer.parseInt(product.getRakutenCatagoryId()));
		
		// Tagline OK
		item.setTagline(product.getTagline());
		
		List <MultiLang> attrVal = new ArrayList<MultiLang>();
		attrVal.add(product.getVariantAttributeName1());
		attrVal.add(product.getVariantAttributeName2());
		item.setVariantLabels(attrVal);
		
		it
		
		// Loop by variants of product
		for(int i=0; i<productVariantList.size(); i++){
			Variants productVariant = productVariantList.get(i);
//			ItemVariant itemVariant = variantsListItem.get(i);
			ItemVariant itemVariant = new ItemVariant();
			// Sku OK
			itemVariant.setSku(productVariant.getSku());

//set images PRODUCT.VARIANTS.IMAGEURL[] SET FIRST VALUE
//			ItemImage itemImage = new ItemImage();
//			itemImage.setLocation(location)		
			if (i==0){
				List<ItemImage> itemImageList = productVariant.getImageURLs();
			}
			
//			// gran
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("gran")){
//				itemVariant.setGran(productVariant.getGlobalTradeItemNumber());
//			}
//			// upc
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("upc")){
//				itemVariant.setUpc(productVariant.getGlobalTradeItemNumber());
//			}
//			// ean
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("ean")){
//				itemVariant.setEan(productVariant.getGlobalTradeItemNumber());
//			}
//			// jan
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("jan")){
//				itemVariant.setJan(productVariant.getGlobalTradeItemNumber());
//			}
//			// gtin
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("gtin")){
//				itemVariant.setGtin(productVariant.getGlobalTradeItemNumber());
//			}
//			// apn
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("apn")){
//				itemVariant.setApn(productVariant.getGlobalTradeItemNumber());
//			}
//			// isbn
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("isbn")){
//				itemVariant.setIsbn(productVariant.getGlobalTradeItemNumber());
//			}
//			// issn
//			if(productVariant.getGlobalTradeItemNumberType().equalsIgnoreCase("issn")){
//				itemVariant.setIssn(productVariant.getGlobalTradeItemNumber());
//			}
			// set weight OK
			ItemWeight shippingWeight = new ItemWeight();
			// set unit code
			shippingWeight.setUnitCode("g");
			shippingWeight.setValue(BigDecimal.valueOf(productVariant.getShippingPackage().getWeight()));
			itemVariant.setShippingWeight(shippingWeight);
			// set width Ok
			ItemDimension itemDimension = new ItemDimension();
			// set unitcode
			itemDimension.setUnitCode("cm");
			itemDimension.setWidth(BigDecimal.valueOf(productVariant.getShippingPackage().getWidth()));
			// set height OK
			itemDimension.setHeight(BigDecimal.valueOf(productVariant.getShippingPackage().getHeight()));
			// set length OK
			itemDimension.setLength(BigDecimal.valueOf(productVariant.getShippingPackage().getLength()));
			itemVariant.setShippingDimension(itemDimension);
			/**************shop info**************/
			Map<String, ItemVariant.ShopInfo> shopInfoMap = new HashMap<String, ItemVariant.ShopInfo>();
			
			ShopInfo shopInfo = new ShopInfo();
			// set duration
			ItemDuration itemDuration = new ItemDuration();
			//itemDuration.setLiveStartTime(product.getDisplayStartDates().get(0).getDisplayStartDate());
			shopInfo.setDuration(itemDuration);
			
			// set price
			List<Price> prices = productVariant.getPrices();
			List<ItemPricing> itemPricingList = new ArrayList<ItemPricing>();			
			for(int j=0; j<prices.size(); j++){
				// shopurl -> shopId
				FindShopInfo findShopInfo = findShopInfoMap.get(prices.get(j).getShopURL());
				//String shopId = findShopInfo.getShop().getShopId();				
				Shop shop = findShopInfo.getShop();				
				
				ItemPricing itemPricing = new ItemPricing();
				itemPricing.setShopId(shop.getShopId());
				itemPricing.setPrice(prices.get(j).getValue());
				itemPricingList.add(itemPricing);
				shopInfo.setPricings(itemPricingList);				
				//shopShippingMethodIds 未設定
				//shopPaymentMethodIds 未設定

				shopInfoMap.put(shop.getShopId(), shopInfo);
			}
			
			itemVariant.setShopInfoMap(shopInfoMap);			
			variantsListItem.add(itemVariant);
		}
		item.setVariants(variantsListItem);
		//object for request
		ItemUpsertRequest itemUpsertRequest = new ItemUpsertRequest();
		itemUpsertRequest.setMerchantId(merchantId);		
		itemUpsertRequest.setItem(item);
		message.setPayload(itemUpsertRequest);
		
		//dummy
//		 message.removeProperty("findShopInfoMap", PropertyScope.SESSION);
		return message;
	} 
}